<?php
class GuestBookAction extends BaseAction {
	//留言板首页
	//用途：显示
	public function index() {
		$page = $this->_get("page");
		$page = $page > 1 ? $page : 1; //如page参数为[字母]或[为空]，情况的默认赋值
		$cid = $this->_get("cid");
		
		$sql = "SELECT * FROM ".C("DB_PREFIX")."guestbook WHERE catalog_id={$cid} ORDER BY timeline DESC";
		$pagination = CommonUtil::getPage($sql,$page,10);
		foreach($pagination ["resultList"] as &$obj){
			$obj["timeline"] = CommonUtil::formatDate($obj["timeline"]);
		}
		$this->assign("pagination",$pagination);
		$this->display();
                
	}
	
	//修改留言，回复留言
	//查看留言详情
	public function reply() {
		$referUrl = $_SERVER['HTTP_REFERER'];
		$id = $this->_get("id");
		$obj = GuestBookHelper::getObject("guestbook_id={$id}");
		
		$this->assign("obj",$obj);
		$this->assign ( "nowTime", time () );
		$this->assign ( "referUrl", $referUrl );
		$this->display ();
	}
	
	//留言板回复
	//用途：回复
	public function reply_process() {
		$id = $this->_post("id");
		$data["reply"] = htmlspecialchars(stripslashes($_POST['reply']));
		if(empty($data["reply"])){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "回复内容不能为空！");
			return;
		}
		$result = GuestBookHelper::update($data, "guestbook_id={$id}" );
		if ($result === false) { //失败，则提示错误信息，并返回上一个页面[记录了上一个页面，用户填写的信息]
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
		} else { //修改成功，提示成功信息，并且返回上一个页面
			echo CommonUtil::ajaxReturn(Status::$SUCCESS, "回复成功！");
		}
	}
	
	public function delete(){
		
		$id = $this->_get("id");
		$obj = GuestBookHelper::getObject("guestbook_id=".$id);
		if(empty($obj)){
			echo CommonUtil::ajaxReturn ( Status::$ERROR, "对象不存在，操作失败！" );
			return ;
		}
		$result = GuestBookHelper::delete ( "guestbook_id=$id" );
		if($result === false){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
			return ;
		}
		echo CommonUtil::ajaxReturn(Status::$RELOAD_PAGE, "操作成功！");
		
	}
	

}